package org.budo.activiti.converter;

import java.util.ArrayList;
import java.util.List;

import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.FormProperty;
import org.activiti.bpmn.model.FormValue;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.editor.language.json.converter.BpmnJsonConverterUtil;
import org.activiti.editor.language.json.converter.util.JsonConverterUtil;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * @author lmw
 */
public class BudoJsonConverterUtil {
    public static void convertJsonToFormProperties_0(JsonNode objectNode, BaseElement element) {
        JsonNode formPropertiesNode = JsonConverterUtil.getProperty(BudoStartEventJsonConverter.PROPERTY_FORM_PROPERTIES, objectNode);
        if (formPropertiesNode != null) {
            formPropertiesNode = BpmnJsonConverterUtil.validateIfNodeIsTextual(formPropertiesNode);
            JsonNode propertiesArray = formPropertiesNode.get("formProperties");
            if (propertiesArray != null) {
                for (JsonNode formNode : propertiesArray) {
                    _convertJsonToFormProperties_1(element, formNode);
                }
            }
        }
    }

    private static void _convertJsonToFormProperties_1(BaseElement element, JsonNode formNode) {
        JsonNode formIdNode = formNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ID);
        if (formIdNode != null && StringUtils.isNotEmpty(formIdNode.asText())) {
            FormProperty formProperty = new FormProperty();
            formProperty.setId(formIdNode.asText());
            formProperty.setName(_getValueAsString_0(BudoStartEventJsonConverter.PROPERTY_FORM_NAME, formNode));
            formProperty.setType(_getValueAsString_0(BudoStartEventJsonConverter.PROPERTY_FORM_TYPE, formNode));
            formProperty.setExpression(_getValueAsString_0(BudoStartEventJsonConverter.PROPERTY_FORM_EXPRESSION, formNode));
            formProperty.setVariable(_getValueAsString_0(BudoStartEventJsonConverter.PROPERTY_FORM_VARIABLE, formNode));

            // 不仅 enum 大家都可以有
            _convertJsonToFormProperties_2(formNode, formProperty);

            if ("date".equalsIgnoreCase(formProperty.getType())) {
                formProperty.setDatePattern(_getValueAsString_0(BudoStartEventJsonConverter.PROPERTY_FORM_DATE_PATTERN, formNode));
            }

            formProperty.setRequired(_getValueAsBoolean_0(BudoStartEventJsonConverter.PROPERTY_FORM_REQUIRED, formNode));
            formProperty.setReadable(_getValueAsBoolean_0(BudoStartEventJsonConverter.PROPERTY_FORM_READABLE, formNode));
            formProperty.setWriteable(_getValueAsBoolean_0(BudoStartEventJsonConverter.PROPERTY_FORM_WRITABLE, formNode));

            if (element instanceof StartEvent) {
                ((StartEvent) element).getFormProperties().add(formProperty);
            } else if (element instanceof UserTask) {
                ((UserTask) element).getFormProperties().add(formProperty);
            }
        }
    }

    private static void _convertJsonToFormProperties_2(JsonNode formNode, FormProperty formProperty) {
        JsonNode enumValuesNode = formNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES);
        if (enumValuesNode != null) {
            List<FormValue> formValueList = new ArrayList<FormValue>();
            for (JsonNode enumNode : enumValuesNode) {
                if (enumNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES_ID) != null //
                        && enumNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES_ID).isNull() == false //
                        && enumNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES_NAME) != null //
                        && enumNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES_NAME).isNull() == false) {

                    FormValue formValue = new FormValue();
                    formValue.setId(enumNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES_ID).asText());
                    formValue.setName(enumNode.get(BudoStartEventJsonConverter.PROPERTY_FORM_ENUM_VALUES_NAME).asText());
                    formValueList.add(formValue);

                } else if (enumNode.get("value") != null && enumNode.get("value").isNull() == false) {
                    FormValue formValue = new FormValue();
                    formValue.setId(enumNode.get("value").asText());
                    formValue.setName(enumNode.get("value").asText());
                    formValueList.add(formValue);
                }
            }
            formProperty.setFormValues(formValueList);
        }
    }

    protected static String _getValueAsString_0(String name, JsonNode objectNode) {
        String propertyValue = null;
        JsonNode propertyNode = objectNode.get(name);
        if (propertyNode != null && propertyNode.isNull() == false) {
            propertyValue = propertyNode.asText();
        }
        return propertyValue;
    }

    protected static boolean _getValueAsBoolean_0(String name, JsonNode objectNode) {
        boolean propertyValue = false;
        JsonNode propertyNode = objectNode.get(name);
        if (propertyNode != null && propertyNode.isNull() == false) {
            propertyValue = propertyNode.asBoolean();
        }
        return propertyValue;
    }
}